package com.example.hyw.szysteam.model;



import android.content.Context;
import android.content.SharedPreferences;
import com.example.hyw.szysteam.POJO.HttpResult;
import com.example.hyw.szysteam.POJO.User;
import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.http.RetrofitManager;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * Created by HYW on 2018/6/4.
 */

public class UserModel {
    public Observable<HttpResult<User>> regist(String email, String password){
        return RetrofitManager.getInstance().getRetrofitApiService().regist(email,password);
    }

    public Observable<HttpResult<User>> login(String email,String password){
        return RetrofitManager.getInstance().getRetrofitApiService().login(email,password);
    }

    public Observable<HttpResult<User>> resetpsw(String email,String password){
        return RetrofitManager.getInstance().getRetrofitApiService().resetpsw(email,password);
    }

    public Observable<Boolean> saveUserInfo(final User user, final Context context){
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                boolean commit = context.getSharedPreferences("UserInfo",Context.MODE_PRIVATE)
                        .edit()
                        .putString("email",user.getEmail())
                        .putString("password",user.getPassword())
                        .commit();
                emitter.onNext(commit);
                emitter.onComplete();
            }
        });
    }

    public Observable<User> getUserInfo(final Context context){
        return Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> emitter) throws Exception {
                User user=new User();
                SharedPreferences sp=context.getSharedPreferences("UserInfo",Context.MODE_PRIVATE);
                user.setEmail(sp.getString("email",""));
                user.setPassword(sp.getString("password",""));
                emitter.onNext(user);
                emitter.onComplete();
            }
        });
    }

    public Observable<HttpResult<User>> editInfo(String email,String name,String school,boolean sex,int age){
        return RetrofitManager.getInstance().getRetrofitApiService().editinfo(email,name,school,sex,age);
    }

    public Observable<Boolean> loginOut(final Context context){
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                SharedPreferences sp =context.getSharedPreferences("UserInfo",Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.clear();
                editor.commit();
                emitter.onNext(true);
            }
        });
    }
}
